//
//  GKZYMyInfoFeedbackViewController.m
//  xxptmobile
//
//  Created by KwokChun Cheung on 15/11/5.
//  Copyright © 2015年 gzedu. All rights reserved.
//

#import "GKZYMyInfoFeedbackViewController.h"
#import "xxptmobile.h"

@interface GKZYMyInfoFeedbackViewController ()<UITextViewDelegate,UITextFieldDelegate>
@property (nonatomic, strong) UITextView *textView;
@property (nonatomic, strong) UITextField *textField;
@property (nonatomic, strong) UILabel *textViewPlaceHolder;
@end

@implementation GKZYMyInfoFeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"意见反馈";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = [UIColor colorWithRed:238/255.f green:238/255.f blue:238/255.f alpha:1];
    
    UIBarButtonItem *submitButtom = [[UIBarButtonItem alloc] init];
    submitButtom.title = @"提交";
    [submitButtom setTarget:self];
    [submitButtom setAction:@selector(toPostSubmit)];
    self.navigationItem.rightBarButtonItem = submitButtom;
    
    [self.view addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(15);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(200));
    }];
    
    [self.view addSubview:self.textField];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.textView.mas_bottom).offset(15);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(40));
    }];
    
}

-(void)toPostSubmit{
    [self.textField resignFirstResponder];
    [self.textView resignFirstResponder];
    
    BOOL isPassContent;
    BOOL isPassPhone;
    NSString *alertMessage = @"信息不正确!";
    
    NSString *regexTel = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    NSPredicate *predicateTel = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regexTel];
    isPassPhone = [predicateTel evaluateWithObject:self.textField.text];
    
    isPassContent = self.textView.text.length <= 4000 && self.textView.text.length > 0;
    
    if (!isPassPhone || !isPassContent) {
        if(!isPassPhone){
            alertMessage = @"手机号码格式不正确!";
        }else if (!isPassContent){
            alertMessage = @"反馈内容长度不符合!";
        }
        [self Alert:alertMessage withComplete:nil];
        return;
    }
    
    
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    InsertFeedbackReq *req = [[InsertFeedbackReq alloc] init];
    req.TERMCOURSE_ID = [userDefaults stringForKey:KEY_TERMCOURSE_ID];
    req.CLASS_ID = [userDefaults stringForKey:KEY_CLASS_ID];
    req.USER_ID = [userDefaults stringForKey:KEY_USER_ID];
    req.FROM_DYNA = [userDefaults stringForKey:KEY_FROMDYNA];
    req.FEEDBACK_CONTENT = [self.textView.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [ApiContactManager toPOST:API_PATH_InsertFeedback andReq:req andContentType:ContentType andSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray *array = (NSArray *)responseObject;
        NSDictionary *dic = array.firstObject;
        if ([dic[@"NUM"] isEqualToNumber:@1]){
            [self Alert:@"感谢您的反馈！" withComplete:^(){
                self.textView.text = @"";
                self.textViewPlaceHolder.hidden = NO;
                [self.textViewPlaceHolder becomeFirstResponder];
                self.textField.text = @"";
            }];
        }
    } andFailure:nil];
}

-(void)Alert:(NSString *)massage withComplete:(void(^)())block {
    UILabel *alertLabel = [[UILabel alloc] init];
    alertLabel.frame = CGRectMake(self.view.frame.size.width/3,self.view.frame.size.height-80 , self.view.frame.size.width/3,20);
    alertLabel.backgroundColor = [UIColor darkGrayColor];
    alertLabel.textAlignment = NSTextAlignmentCenter;
    alertLabel.text = massage;
    alertLabel.layer.cornerRadius = 2.0;
    alertLabel.layer.masksToBounds = YES;
    alertLabel.textColor = [UIColor whiteColor];
    alertLabel.font = [UIFont systemFontOfSize:12.0];
    [self.view addSubview:alertLabel];
    [UIView animateWithDuration:1.2 animations:^{
        alertLabel.alpha = 0.0;
    } completion:^(BOOL finished) {
        [alertLabel removeFromSuperview];
        if (block) { block(); }
    }];
}

#pragma mark - Getter For Views

-(UITextView *)textView{
    if (!_textView) {
        _textView = ({
            UITextView *view = [UITextView new];
            view.font = [UIFont systemFontOfSize:15];
            view.delegate = self;
            
            UILabel *placeHolder = [UILabel new];
            placeHolder.text = @"请输入你的反馈内容";
            placeHolder.textColor = [UIColor lightGrayColor];
            placeHolder.font = [UIFont systemFontOfSize:15];
            [placeHolder sizeToFit];
            self.textViewPlaceHolder = placeHolder;
            [view addSubview:placeHolder];
            [placeHolder mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(view).offset(8);
                make.left.equalTo(view).offset(5);
            }];
            
            view;
        });
    }
    return _textView;
}

-(UITextField *)textField{
    if (!_textField) {
        _textField = ({
            UITextField *field = [UITextField new];
            field.placeholder = @"手机或QQ,以便我们必要时与您联系";
            field.backgroundColor = [UIColor whiteColor];
            field.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
            field.leftViewMode = UITextFieldViewModeAlways;
            field.font = [UIFont systemFontOfSize:15];
            field.delegate = self;
            field;
        });
    }
    return _textField;
}

-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length <= 0) {
        [self.textViewPlaceHolder setHidden:NO];
    }else{
        [self.textViewPlaceHolder setHidden:YES];
    }
}
@end
